import sqlite

class dbreader():
    def __init__(self, inpath):
        databaseFile="fsm.sqlite"
        con = sqlite.connect(databaseFile)
        self.cur = con.cursor()

        tempgdp = self.selectAll('gdp')
        self.gdp=self.tranformgdpData(tempgdp)
        self.pop2001 = self.selectAll('Economic_dimoi_2001')
        self.pop1991 = self.selectAll('Economic_dimoi_1991')
        
        self.compTypes = self.selectAll('types')
        tempComp = self.selectAll('companies')
        self.comp=self.tranformCompaniesData(tempComp)
        
        self.areas = self.selectAll('areas')
        
        

    def selectAll(self,inTable):
        sql=("select * from '%s'") % inTable
        self.cur.execute(sql)
        return list( self.cur.fetchall())
    
    def tranformCompaniesData(self,inData):
        area="83010000"
        #return [  [str(x[0]),x[2]] for x in inData if (str(x[3])==area) ]
        c= [  x[2] for x in inData if (str(x[3])==area) ]
        return reduce(lambda x, y: x + y, c)
    
    def tranformgdpData(self,ingdpData):
        result=[]
        for i in ingdpData:
            result.append([i[0],i[1]])
        return result
    


